/**
 * Created by chenzheyu on 2017/4/5.
 */


$(function () {
    var option = {
        nameOrPhone: '',
        page: 1,
        limit: 20,
        appkey: 'cb904bd8a16d4511bbd826d68e554098'
    };


    function PagingMd(index, SEOVal, Refresh) {
        option = {
            nameOrPhone: SEOVal || '',
            page: index,
            rows: 20,
            appkey: 'cb904bd8a16d4511bbd826d68e554098'
        };
        $.ajax({
            url: 'http://192.168.0.73:9099/api/information/selectInforMation',
            data: option,
            type: 'GET',
            dataType: "json",
            success: function (data) {
                var Data = data;
                var OrderOption = {//表格数据参数
                    data: {
                        Thead: {
                            result: '是否通过',
                            name: '姓名',
                            phone: '手机',
                            idcard: '身份证',
                            drivernumber: '驾驶证',
                            photourl: '证件照',
                            registrationtime: '注册时间',
                            reviewer: '审核人',
                            action: '操作'
                        },
                        Tbody: []
                    },
                    OrderH:'500px',
                    select: true,
                    selectchange: function (index) {//多选
                        var IdPass = [];
                        for (var i = 0; i < index.length; i++) {
                            IdPass.push(Data.data.rows[i].id)
                        }
                        var IdPassSt='';
                        if(IdPass.length>1){
                            IdPassSt = IdPass.join(",");
                        }else {
                            IdPassSt+=IdPass[0]
                        }
                        $('.BatchPass').on('click', function (e) {
                            e.stopPropagation();
                            $.ajax({
                                url: "http://192.168.0.73:9099/api/information/BatchPass",
                                type: 'GET',
                                data: {
                                    appkey: 'cb904bd8a16d4511bbd826d68e554098',
                                    size: IdPassSt
                                },
                                success: function (data) {
                                    PagingMd(1)
                                }
                            })
                        })

                    }
                };


                var PagingMdOption = {
                    data: {
                        rows: 20,
                        nowrows: index,
                        Pages: '',
                        total:data.data.total,
                        change: function (index) {
                            PagingMd(index);
                        }
                    }
                };
                if (data.message == '成功') {
                    PagingMdOption.data.Pages = Math.ceil(data.data.total / PagingMdOption.data.rows);
                    for (var i = 0; i < data.data.rows.length; i++) {
                        var val = {
                            id: data.data.rows[i].id,
                            result: data.data.rows[i].driverresult==1 && data.data.rows[i].idcardbackresult==1 && data.data.rows[i].idcardpositiveresult==1 ? '通过' : '未通过',
                            name: data.data.rows[i].name,
                            phone: data.data.rows[i].phone,
                            idcard: data.data.rows[i].idcard,
                            driver: data.data.rows[i].drivernumber,
                            photourl: '',
                            registrationtime: '',
                            reviewervarchar: data.data.rows[i].reviewer,
                            action: '<button class="AuditButton">审核</button>'
                        };
                        photourls = data.data.rows[i].photourl.split(',');
                        function getLocalTime(nS) {
                            return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
                        }

                        val.registrationtime = getLocalTime(data.data.rows[i].created);
                        val.photourl = '<div class="CertificateImg">';
                        for (var j = 0; j < photourls.length; j++) {
                            val.photourl += '<img  src="' + photourls[j] + '">';
                        }
                        val.photourl += '</div>';
                        OrderOption.data.Tbody.push(val)
                    }
                }

                if (!Refresh) {//要不要刷新
                    $('.Top').find('.CustomOrder').remove();
                    $('.Top').parent().find('.PagingMdPagingMd').remove();
                }

                $('.Top').Order(OrderOption);
                $('.Top').parent().PagingMd(PagingMdOption);
                $('.BatchPass').css({display:'block'});
                var AuditButton = $('.AuditButton');
                for (var i = 0; i < AuditButton.length; i++) {
                    AuditButton.eq(i).on('click', function () {
                        var obj = {
                            id: $(this).parent().parent().data('id'),
                            appkey: option.appkey
                        };
                        Audit(obj)
                    })
                }

            },
            error: function () {
                $('.Top').find('.CustomOrder').remove();

                var OrderOption = {//表格数据参数
                    data: {
                        Thead: {
                            result: '是否通过',
                            phone: '手机',
                            name: '姓名',
                            idcard: '身份证',
                            drivernumber: '驾驶证',
                            photourl: '证件照',
                            registrationtime: '注册时间',
                            reviewer: '审核人',
                            action: '操作'
                        },
                        Tbody: []
                    },
                    OrderH: '500px'
                };
                $('.Top').Order(OrderOption);
                $('.BatchPass').css({display:'none'});
            }

        })
    }

    PagingMd(1);

    $('.SE0Button').on('click', function () {
        PagingMd(1, $('.SEOVal').val());
    });

    function Audit(boj) {
        $.ajax({
            url: 'http://192.168.0.73:9099/api/information/selectById',
            data: boj,
            type: 'GET',
            dataType: "json",
            success: function (data) {
            }
        })
    }
});
